package com.sls.web.manager.action.game;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GamePriceTemplate;
import com.sls.web.manager.game.querybean.GamePriceTemplateQuery;
import com.sls.web.manager.game.service.GamePriceTemplateService;
import com.sls.web.manager.page.Pagination;

/**
 * 价格模板 ：模板列表显示，添加，修改，详情。
 * 
 * @author gesx
 * 
 */
public class GamePriceTemplateAction extends SuperAction {
	private static final long serialVersionUID = 6636175660447974155L;
	@Resource(name = "gamePriceTemplateService")
	private GamePriceTemplateService gamePriceTemplateService;
	private GamePriceTemplateQuery gamePriceTemplateQuery;
	private GamePriceTemplate gamePriceTemplate;
	private Pagination<?> page;
	private String templateCode;
	private String templateName;
	private String tempCode;
	/**
	 * 列表显示
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (gamePriceTemplateQuery == null) {
				gamePriceTemplateQuery = new GamePriceTemplateQuery();
				gamePriceTemplateQuery.setPageSize(Constants.INIT_PAGESIZE);
				gamePriceTemplateQuery
						.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String templateCode = gamePriceTemplateQuery.getTemplateCode();
			String templateName = gamePriceTemplateQuery.getTemplateName();
			templateCode = FilterStringSpaces.checkStringHasSpace(templateCode);
			templateName = FilterStringSpaces.checkStringHasSpace(templateName);
			gamePriceTemplateQuery.setTemplateCode(templateCode);
			gamePriceTemplateQuery.setTemplateName(templateName);
			page = gamePriceTemplateService.queryList(gamePriceTemplateQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}

		return LIST;

	}

	/**
	 * 跳转添加页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {

		return INPUT;

	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			gamePriceTemplate.setTemplateName(gamePriceTemplate.getTemplateName().trim());
			gamePriceTemplateService.save(gamePriceTemplate,
					getCurrentOperator());
			redirectionUrl = "game_price_template!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 跳转修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		if (StringUtils.isNotEmpty(templateCode)) {
			gamePriceTemplate = gamePriceTemplateService.queryByCode(
					templateCode, getCurrentOperator());
			if (gamePriceTemplate == null) {
				return ERROR;
			}
		}
		if (StringUtils.isNotEmpty(templateCode)) {
			return INPUT;
		}

		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {

		try {
			gamePriceTemplate.setTemplateName(gamePriceTemplate
					.getTemplateName().trim());
			gamePriceTemplate.setRemark(gamePriceTemplate.getRemark().trim());
			gamePriceTemplateService.update(gamePriceTemplate,
					getCurrentOperator());
			if (gamePriceTemplateQuery == null) {
				gamePriceTemplateQuery = new GamePriceTemplateQuery();
				gamePriceTemplateQuery.setPageSize(Constants.INIT_PAGESIZE);
				gamePriceTemplateQuery
						.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			if (StringUtils.isNotEmpty(templateName)) {
				gamePriceTemplateQuery.setTemplateName(gamePriceTemplate.getTemplateName());
			}
			if (StringUtils.isNotEmpty(tempCode)) {
				gamePriceTemplateQuery.setTemplateCode(tempCode);
			}
			return list();
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
			redirectionUrl = "game_price_template!list.htm";
			return SUCCESS;
		}
	}

	/**
	 * 根据模板名称查询单条记录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryByCode() throws Exception {

		try {
			gamePriceTemplate = gamePriceTemplateService.queryByCode(
					templateCode, getCurrentOperator());
			if (gamePriceTemplate == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "detail";

	}

	public GamePriceTemplateQuery getGamePriceTemplateQuery() {
		return gamePriceTemplateQuery;
	}

	public void setGamePriceTemplateQuery(
			GamePriceTemplateQuery gamePriceTemplateQuery) {
		this.gamePriceTemplateQuery = gamePriceTemplateQuery;
	}

	public GamePriceTemplateService getGamePriceTemplateService() {
		return gamePriceTemplateService;
	}

	public void setGamePriceTemplateService(
			GamePriceTemplateService gamePriceTemplateService) {
		this.gamePriceTemplateService = gamePriceTemplateService;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public GamePriceTemplate getGamePriceTemplate() {
		return gamePriceTemplate;
	}

	public void setGamePriceTemplate(GamePriceTemplate gamePriceTemplate) {
		this.gamePriceTemplate = gamePriceTemplate;
	}

	public String getTemplateCode() {
		return templateCode;
	}

	public void setTemplateCode(String templateCode) {
		this.templateCode = templateCode;
	}

	public String getTemplateName() {
		return templateName;
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

	public String getTempCode() {
		return tempCode;
	}

	public void setTempCode(String tempCode) {
		this.tempCode = tempCode;
	}

}
